package com.chinasofti.学习;

import java.util.ArrayList;
import java.util.OptionalLong;
import java.util.Scanner;

public class StudentManager {
    public static void main(String[] args) {
        Scanner sc =new Scanner(System.in);
        ArrayList<Student>list = new ArrayList<>();
        ol:while (true){
            System.out.println("--------------------学生管理系统--------------------");
            System.out.println("1.添加学生");
            System.out.println("2.删除学生");
            System.out.println("3.修改学生");
            System.out.println("4.查看学生");
            System.out.println("5.退出");
            System.out.println("-------------------请输入您得选择--------------------");
            String choice = sc.next();
            switch (choice){
                case "1":
                    //System.out.println("添加学生");
                    addStudent(list);
                    break;
                case "2":
                    //System.out.println("删除学生");
                    deletStudent(list);
                    break;
                case "3":
                    System.out.println("修改学生");
                    break;
                case "4":
                    //System.out.println("查看学生");
                    queryStudent(list);
                    break;
                case "5":
                    System.out.println("退出");
                    break ol;
                    default:
                        System.out.println("你的输入有误请重新输入");
            }
        }
    }
    //添加学生
       public static void  addStudent(ArrayList<Student>list){
        Scanner sc = new Scanner(System.in);
           String  sid;
           while (true){
               System.out.println("请输入您的学号:");
               sid =sc.next();
               int index = getIndex(list,sid);
               if (index==-1){
                   break;
               }
           }
           System.out.println("请输入您的姓名：");
            String name = sc.next();
           System.out.println("请输入您的年龄：");
           int age= sc.nextInt();
           System.out.println("请输入您的生日：");
           String birthday=sc.next();
           Student stu = new Student(sid,name,age,birthday );
           list.add(stu);
           System.out.println("添加成功");

       }
       //删除
    public static void deletStudent(ArrayList<Student>list){
        Scanner sc = new Scanner (System.in);
        System.out.println("请输入要删除的学号");
        String sid = sc.next();
        int index =getIndex(list,sid);
        if (index==-1){
            System.out.println("查无信息请重新录入");
        }else {
            list.remove(index);
            System.out.println("删除成功");
        }
    }
    //查看学生
    public static  void queryStudent(ArrayList<Student>list){
        if(list.size()==0){
            System.out.println("查无信息请重新录入");
            return;
        }
            for (int i = 0; i <list.size() ; i++) {
               Student s = list.get(i);
                System.out.println("学号："+s.getSid()+"   "+"姓名："+s.getName()+"   "+"年龄："+s.getAge()+"岁"+"   "+"生日："+s.getBirthday());
            }
        }
        //修改学生
    public  static void updateStudent(ArrayList<Student>list){
        Scanner sc = new Scanner (System.in);
        System.out.println("请输入要修改的学生学号");
        String upadteSid =sc.next();
        int index = getIndex(list,upadteSid);
        if(index==-1){
            System.out.println("查无信息请重新录入");
        }
        else {
            Student stu = list.get(index);
            System.out.println("信息如下姓名："+stu.getName()+"年龄,"+stu.getAge()+",生日："+stu.getBirthday());
            System.out.println("请输入您的姓名:");
            String name = sc.next();
            System.out.println("请输入您的年龄:");
            int age = sc.nextInt();
            System.out.println("请输入您的生日:");
            String birthday = sc.next();
            Student st = new Student(upadteSid,name,age,birthday);
            list.set(index,st);
            System.out.println("修改成功");
        }
    }

    private static int getIndex(ArrayList<Student> list, String sid) {
        int  index = -1;
        for (int i = 0; i <list.size() ; i++) {
           Student s =list.get(i);
           String id = s.getSid();
           if (id.equals(sid)){
               index=i;
           }
        }
        return index;
    }


}
